/**
 * This file is part of Wasagent.
 *
 * Wasagent is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wasagent is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wasagent. If not, see <http://www.gnu.org/licenses/>.
 * 
 *
 * WasAgentServer.java: Main daemon that handles the incoming connections
 *                       
 * 
 *
 *
 */

import net.wait4it.wasagent.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;


/**
 *
 * @author Patricio Dorantes Jamarne
 *
 *
 */

public class WasAgentServer {


    public static void main(String[] args) throws Exception {


        ArrayList<WasModule> wasmodules;

        int            port = Integer.parseInt(args[0]);
        boolean        stop = false;        


        Socket         socket;
        ServerSocket   serviceSocket;
//      WasModule      module = new WasModule();
        wasmodules = new ArrayList<WasModule>();

        System.out.println("WasAgentServer.java: Server starting");

        try {
            serviceSocket = new ServerSocket(port);
            while(!stop) {
                       
                 try {
                     socket = serviceSocket.accept();
                     // System.out.println("Connection From: " + socket.getInetAddress() + ":"+socket.getPort()); 
                    
                     HandleSocket hs = new HandleSocket(socket,wasmodules);
                     hs.start();

                 }
                 catch(Exception e) {
                     System.out.println(e.toString());
                 }
            }
        }
        catch(Exception e) {
            System.out.println(e.toString());
            stop = true;
        }

            
    }

}
